//package com.gxitsky.plugin;

import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * @author gxing
 * @desc TODO
 * @date 2021/12/31
 */
//public class MainTest {
//
//    public static void main(String[] args) throws ClassNotFoundException, IOException {
//        Class<Base64Encode> base64EncodeClass = Base64Encode.class;
//        Class<IFunction> iFunctionClass = IFunction.class;
//        boolean assignableFrom = iFunctionClass.isAssignableFrom(base64EncodeClass);
//        System.out.println(assignableFrom);

//        ClassLoader classLoader = MainTest.class.getClassLoader();
//        Class<?> iFunctionClass = classLoader.loadClass("com.gxitsky.plugin.IFunction");
//        Class<?> base64Encode = classLoader.loadClass("com.gxitsky.plugin.Base64Encode");
//        boolean assignableFrom1 = iFunctionClass.isAssignableFrom(base64Encode);
//        System.out.println(assignableFrom1);

//        URL url = new URL("file:///D:/service-impl-1.0.0-SNAPSHOT.jar");
//        URLClassLoader loader = new URLClassLoader(new URL[]{url});
//        Class<?> aClass = loader.loadClass("com.gxitsky.plugin.Base64Encode");
//        boolean assignableFrom2 = iFunctionClass.isAssignableFrom(aClass);
//        System.out.println(assignableFrom2);

//        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
//        URL url4 = new URL("jar:file:///D:/service-impl-1.0.0-SNAPSHOT.jar!/");
//        URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{url4});
//
//        System.out.println("contextClassLoader: " + contextClassLoader.toString());
//
//        JarURLConnection jarURLConnection = (JarURLConnection) url4.openConnection();
//        JarFile jarFile = jarURLConnection.getJarFile();
//        Enumeration<JarEntry> entries = jarFile.entries();
//        while (entries.hasMoreElements()) {
//            JarEntry jarEntry = entries.nextElement();
//            String entryName = jarEntry.getName();
//            String className = entryName.replace("/", ".");
//            if (jarEntry.isDirectory() || !className.startsWith("com.gxitsky.plugin") || !className.endsWith(".class")) {
//                continue;
//            }
//            String classPath = className.substring(0, entryName.length() - 6);
//            Class<?> clazz = urlClassLoader.loadClass(classPath);
//            boolean anInterface = clazz.isInterface();
//            boolean assignableFrom3 = IFunction.class.isAssignableFrom(clazz);
//            System.out.println(className + ": " + assignableFrom3);
//        }

//        URLClassLoader loader4 = new URLClassLoader(new URL[]{url});
//        Class<?> aClass4 = loader.loadClass("com.gxitsky.plugin.Base64Encode");
//        boolean assignableFrom4 = iFunctionClass.isAssignableFrom(aClass);
//        System.out.println(assignableFrom4);
//    }
//}
